package keeto.com.vn.classified.adapter;

import java.util.ArrayList;

import keeto.com.vn.classified.network.entity.ListItemEntity;
import keeto.com.vn.classified.ui.IADetailProductActivity;
import keeto.com.vn.classified.ui.R;
import keeto.com.vn.classified.utils.StringUtilities;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.androidquery.AQuery;


public class AdapterProductViewAll extends BaseAdapter {
	private Context context;
	private ArrayList<ListItemEntity> List;
	private LayoutInflater  inflater;
	private AQuery mQuery ;

	
	public AdapterProductViewAll(Context context, ArrayList<ListItemEntity> list, AQuery query) {
		super();
		this.context = context;
		this.List = list;
		this.inflater=LayoutInflater.from(context);
		this.mQuery = query;
	}


	class ViewHolder {
		private ImageView iv_img_product;
		private TextView tv_name_product;
		private TextView tv_content_product;
		private TextView tv_price_product;
		private TextView tv_home_discount;
		private RatingBar rb_ratingBar_product;
	}
	


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return List.size();
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return List.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null) {
			convertView = inflater.inflate(R.layout.adapter_rowview,parent, false);
			holder = new ViewHolder();
			holder.iv_img_product = (ImageView) convertView.findViewById(R.id.img_product);
			holder.tv_name_product = (TextView) convertView.findViewById(R.id.name_product);
			holder.tv_content_product= (TextView) convertView.findViewById(R.id.content_product);
			holder.tv_price_product = (TextView) convertView.findViewById(R.id.price_product);
			holder.tv_home_discount = (TextView) convertView.findViewById(R.id.tv_home_discount);
			holder.tv_home_discount.setVisibility(View.GONE);
			holder.rb_ratingBar_product = (RatingBar) convertView.findViewById(R.id.ratingBar_product);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ListItemEntity product = List.get(position);
		
		if(product.getCoverPicture().size() > 0 && !TextUtils.isEmpty(product.getCoverPicture().get(0))){
			String url = product.getCoverPicture().get(0);
			mQuery.id(holder.iv_img_product).image(url, true, true, 0, 0, null, 0, 1.0f);
		} else {
			mQuery.id(holder.iv_img_product).image(R.drawable.no_image);

		}
		
		String name_product = product.getTitle();
		holder.tv_name_product.setText( product.getTitle());
		String subDec = product.getSubDec();
		if(subDec.length() >0){
			subDec.replace("\n", "");
			subDec.replace("\r", "");
			subDec.replace("\t", "");
		}
		if(subDec.length() >60){
			subDec = subDec.substring(0,57)+"...";
		} 
		holder.tv_content_product.setText(""+Html.fromHtml(subDec.trim()));
		Log.e("TAGGGGGGFFFFFF", ""+Html.fromHtml(subDec.trim()));
		String price = StringUtilities.convertPrice(product.getPrice());
		
		if(product.getLisPromotionHomeEntities() != null){
			if(!product.getLisPromotionHomeEntities().equals("0") && !product.getLisPromotionHomeEntities().equals("")){
				holder.tv_home_discount.setText("-" + product.getLisPromotionHomeEntities().getDiscount() +"%");
				holder.tv_home_discount.setVisibility(View.VISIBLE);
				String new_price = StringUtilities.convertNewPrice(product.getPrice(), Integer.parseInt(product.getLisPromotionHomeEntities().getDiscount()));
				if (!TextUtils.isEmpty(new_price) && !new_price.equals("0")) {
					holder.tv_price_product.setText(new_price + " VND");
					holder.tv_price_product.setVisibility(View.VISIBLE);
				} else {
					holder.tv_price_product.setVisibility(View.INVISIBLE);
				}
			} else {
				holder.tv_home_discount.setVisibility(View.GONE);
				if (!TextUtils.isEmpty(price) && !price.equals("0")) {
					holder.tv_price_product.setVisibility(View.VISIBLE);
					holder.tv_price_product.setText(price + " VND");
				} else {
					String additionPrice = product.getAdditionPrice();
					if(additionPrice != null && additionPrice.length() > 0){
						holder.tv_price_product.setText(Html.fromHtml(additionPrice));
						holder.tv_price_product.setVisibility(View.VISIBLE);
					}else{
						holder.tv_price_product.setVisibility(View.INVISIBLE);
					}
				}
			}
		} else {
			if(!product.getPrice().equals("0")){
				holder.tv_price_product.setText(StringUtilities.convertPrice(product.getPrice())+" VND");
			} else {
				holder.tv_price_product.setText(product.getAdditionPrice());
			}
			holder.tv_price_product.setVisibility(View.VISIBLE);
		}
		
		float rating = product.getRating();
		holder.rb_ratingBar_product.setRating(rating);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				IADetailProductActivity.actionLaunchProduct(context, product);
			}
		});
		
		return convertView;
	}
}
